//
//  GiftControllerd.m
//  Gift
//
//  Created by dllo on 15/9/10.
//  Copyright (c) 2015年 dllo. All rights reserved.
//

#import "GiftControllerd.h"
#import "BaseHeader.h"


@interface GiftControllerd ()<UIScrollViewDelegate,UICollectionViewDataSource,UICollectionViewDelegate,pushTwo,pushOne>
//数组
@property (nonatomic, retain)NSMutableArray *array;
@property (nonatomic, retain)NSMutableArray *buttonArr;
@property (nonatomic, retain)NSArray *faceArray;

@property (nonatomic, retain)UIScrollView *titleScrollView;
@property (nonatomic, retain)UICollectionView *collectionView;
@property (nonatomic, retain)UIButton *button;

@end

@implementation GiftControllerd

-(void)dealloc{
    
    self.titleScrollView.delegate = nil;
    self.collectionView.delegate = nil;
    self.collectionView.dataSource = nil;
    [[NSNotificationCenter defaultCenter] removeObserver:self];
    [_array release];
    [_buttonArr release];
    [_faceArray release];
    [_titleScrollView release];
    [_collectionView release];
    [_button release];
    [super dealloc];
    
}

#pragma mark - 创建六个标题按钮,实现上下连动
-(void)createTitleButton{
    //创建放 Button 的scrollView
    self.titleScrollView = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 0, 360, 40)];
    [self.view addSubview:self.titleScrollView];
    self.titleScrollView.backgroundColor = [UIColor whiteColor];
    self.titleScrollView.contentSize = CGSizeMake(SCREEN_WIDTH + 125, 0);
    self.titleScrollView.showsHorizontalScrollIndicator = NO;
    //创建button
    self.buttonArr = [[NSMutableArray alloc]init];
    NSArray *arr = @[@"精选", @"礼物", @"美食", @"数码", @"家居", @"运动", @"涨姿势"];
    for (int i = 0; i < 7; i++) {
        self.button = [UIButton buttonWithType:UIButtonTypeCustom];
        self.button.frame = CGRectMake(20 + 60 * i, 10, 70, 20);
        [self.button setTitle:[arr objectAtIndex:i] forState:UIControlStateNormal];
        [self.button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        [self.titleScrollView addSubview:self.button];
        self.button.titleLabel.font = [UIFont systemFontOfSize:17.];
        self.button.tag = 1000 + i;
        [self.button addTarget:self action:@selector(liandong:) forControlEvents:UIControlEventTouchUpInside];
        [self.buttonArr addObject:self.button];
        

    }
}


#pragma mark - 上下连动的Button的点击方法
-(void)liandong:(UIButton *)sender{
    //上下连动
    for (int i = 0; i < self.buttonArr.count; i++) {
        if (sender == [self.buttonArr objectAtIndex:i]) {
            sender.titleLabel.font = [UIFont systemFontOfSize:22.];
            [sender setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
            self.collectionView.contentOffset = CGPointMake(SCREEN_WIDTH * i, 0);
            if (i < self.buttonArr.count - 3 && i != 0) {
                self.titleScrollView.contentOffset = CGPointMake((i - 1) * 75 , 0);
            }else if (i >= self.buttonArr.count - 3){
                self.titleScrollView.contentOffset = CGPointMake((self.buttonArr.count - 4) * 50, 0);
            }
        }else{
            UIButton *otherButton = [self.buttonArr objectAtIndex:i];
            otherButton.titleLabel.font = [UIFont systemFontOfSize:17.];
            [otherButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        }
    }
}


#pragma mark - scrollView代理方法 上下连动
-(void)scrollViewDidScroll:(UIScrollView *)scrollView{
    if ([self.titleScrollView isEqual:scrollView]) {
        
    }else if ([self.collectionView isEqual:scrollView]){
        //下上连动
    for (int i = 0; i < self.buttonArr.count; i++) {
        if (self.collectionView.contentOffset.x == SCREEN_WIDTH * i) {
            UIButton *button = self.buttonArr[i];
            button.titleLabel.font = [UIFont systemFontOfSize:22.];
            [button setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
            if (i < self.buttonArr.count - 3 && i != 0) {
                [self.titleScrollView setContentOffset:CGPointMake((i - 1) * 75, 0)];
            }else if (i >= self.buttonArr.count - 3){
                [self.titleScrollView setContentOffset:CGPointMake((self.buttonArr.count - 4) * 50, 0)];
                }
            }else{
                UIButton *otherButton = self.buttonArr[i];
                otherButton.titleLabel.font = [UIFont systemFontOfSize:17.];
                [otherButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
            }
        }
    }
}


#pragma mark - 创建瀑布流
- (void)createCollection{
    UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];
    
    flowLayout.minimumInteritemSpacing = 0;
    flowLayout.minimumLineSpacing = 0;
    flowLayout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
    flowLayout.sectionInset = UIEdgeInsetsMake(0, 0, 0, 0);
    
    flowLayout.itemSize = CGSizeMake(SCREEN_WIDTH, SCREEN_HEIGHT - 153);
    self.collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 40, SCREEN_WIDTH , SCREEN_HEIGHT - 153) collectionViewLayout:flowLayout];
    self.collectionView.pagingEnabled = YES;
    self.collectionView.delegate = self;
    self.collectionView.dataSource = self;
    [self.view addSubview:_collectionView];
    self.collectionView.backgroundColor = [UIColor whiteColor];
    
    [self.collectionView registerClass:[LDCollectionCellOne class] forCellWithReuseIdentifier:@"LDcollectionOne"];
    [self.collectionView registerClass:[LDCollectionCellTow class ] forCellWithReuseIdentifier:@"LDCollectionCellTow"];
    [flowLayout release];
    [_collectionView release];
    
    
}

#pragma mark - 必须执行的collection代理方法
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return 7;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.row == 0) {
    LDCollectionCellOne *cellOne = [collectionView dequeueReusableCellWithReuseIdentifier:@"LDcollectionOne" forIndexPath:indexPath];
        cellOne.dataStr = [self.faceArray objectAtIndex:indexPath.row];
        cellOne.delegate = self;

        
    return cellOne;
    }else if (indexPath.row > 0 && indexPath.row < 7){
        
    LDCollectionCellTow *cellTow = [collectionView dequeueReusableCellWithReuseIdentifier:@"LDCollectionCellTow" forIndexPath:indexPath];
    cellTow.dataStr = [self.faceArray objectAtIndex:indexPath.row];
        
        cellTow.delegate = self;
    return cellTow;
     }
    return nil;
}

- (void)getPushNextPage:(NSString *)str Model:(GiftModel *)model{
    LDWebController *ldWebTow = [[LDWebController alloc] init];
    
    ldWebTow.hidesBottomBarWhenPushed = YES;
    ldWebTow.str = str;
    ldWebTow.model = model;
    self.tabBarController.tabBar.hidden = YES;
    [self.navigationController pushViewController:ldWebTow animated:YES];

    [ldWebTow release];
}

- (void)getPushNextPageOne:(NSString *)str Model:(GiftModel *)model{
    
    LDWebController *ldWebOne = [[LDWebController alloc]init];
    ldWebOne.hidesBottomBarWhenPushed = YES;
    ldWebOne.str = str;
    self.tabBarController.tabBar.hidden = YES;
    [self.navigationController pushViewController:ldWebOne animated:YES];
    ldWebOne.model = model;
    [ldWebOne release];
}

-(void)viewWillAppear:(BOOL)animated{
    self.tabBarController.tabBar.hidden = NO;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self.view setBackgroundColor:[UIColor clearColor]];
    self.automaticallyAdjustsScrollViewInsets = NO;
    self.navigationController.automaticallyAdjustsScrollViewInsets = NO;
    self.title = @"礼物";
    self.faceArray = [@[@"100",@"128",@"118",@"121",@"111",@"123",@"120",]mutableCopy];
    
    
    [self createCollection];
    [self createTitleButton];
    [self night];
   
        
        
    

}

//夜间
- (void)night{
    
    NSUserDefaults *user =  [NSUserDefaults standardUserDefaults];
    NSString *passWord = [user objectForKey:@"night"];
    if ([passWord isEqualToString:@"yes"]) {
        [self.button setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(handleNotification:) name:@"lidi" object:nil];
        [self.collectionView reloadData];
    }
}

#pragma mark - 夜间模式
- (void)handleNotification:(NSNotification *)Notification{
    
    NSString *passWord = [Notification object];
    if ([passWord isEqualToString:@"yes"]) {
        self.collectionView.backgroundColor = [UIColor whiteColor];
        [self.collectionView reloadData];
    }else if ([passWord isEqualToString:@"no"]){
        self.collectionView.backgroundColor = [UIColor blackColor];
        [self.collectionView reloadData];
    }
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
